package jianda;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;

public class Jianda1 {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException, TransformerException {

        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

        DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();

        Document doc=documentBuilder.parse("src/jianda/StudentScores.xml");

        NodeList student = doc.getElementsByTagName("student");
//
//        for (int i = 0; i < student.getLength(); i++) {
//            Node stuItem = student.item(i);
//
//            NodeList childNodes = stuItem.getChildNodes();
//
//            for (int j = 0; j < childNodes.getLength(); j++) {
//                Node item = childNodes.item(j);
//
//                if(item.getNodeType() == Node.ELEMENT_NODE) {
//
//                    Element element = (Element) item;
//
//                    if (element.getNodeName().equals("name")) {
//
//                        System.out.println("姓名:" + element.getTextContent());
//
//                    }
//                    if (element.getNodeName().equals("course")) {
//
//                        System.out.println("课程:" + element.getTextContent());
//
//                    }
//                    if (element.getNodeName().equals("score")) {
//
//                        System.out.println("分数:" + element.getTextContent());
//
//                    }
//                }
//            }
//        }
        //3_2
//        for (int i = 0; i < student.getLength(); i++) {
//            Node studentNode = student.item(i);
//            Element studentElement = (Element)studentNode;
//            if(studentElement.getAttribute("id").equals("2")){
//                NodeList childNodes = studentNode.getChildNodes();
//                for (int j = 0; j < childNodes.getLength(); j++) {
//                    Element element = (Element) childNodes.item(j);
//                    if (element.getNodeName().equals("score") ) {
//                        element.setTextContent("60");
//                    }
//
//                }
//            }
//        }
        //3_1
        for (int i = 0; i < student.getLength(); i++) {
            Node studentNode = student.item(i);
            Element studentElement = (Element) studentNode;
            if (studentElement.getAttribute("id").equals("1")) {
                NodeList childNodes = studentElement.getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    if (childNodes.item(j).getNodeType() == Node.ELEMENT_NODE) {
                        if (childNodes.item(j).getNodeName().equals("score")) {
                            Element element = (Element) childNodes.item(j);
                            element.getParentNode().removeChild(element);
                        }

                    }
                }
            }
        }

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(doc);
        //设置编码类型
        transformer.setOutputProperty(OutputKeys.ENCODING, "gb2312");
        StreamResult result = new StreamResult(new FileOutputStream("src/jianda/StudentScores.xml"));
        //把DOM数转换为XML文件
        transformer.transform(domSource, result);
    }
}
